/**
 * JAVACC DEMO 1.0
 */
package com.apache.info.template;

import com.apache.api.vo.ParamsVo;
import com.apache.database.constant.SpringContextLoader;
import com.apache.info.manager.PubMetadataManager;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.info.template.helper.*;
import com.apache.info.util.FileOperate;
import com.apache.uct.common.ToolsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description: 生成sql
 *
 * @author dyhou 创建时间：2016-9-29
 */
public class BuildProjectFactory {

    private Logger log = LoggerFactory.getLogger(getClass());

    private PubSortInfoManager sortInfoManager;// = LoadManagerFactory.getInstance().getManager("systemModelManager");

    private PubMetadataManager metadataManager;

    private static BuildProjectFactory instance;

    private Map<String, TemplateManager> temps = new HashMap<String, TemplateManager>();

    private BuildProjectFactory() {
        sortInfoManager = (PubSortInfoManager) SpringContextLoader.getBean("pubSortInfoManager");
        metadataManager = (PubMetadataManager) SpringContextLoader.getBean("pubMetadataManager");
        temps.put("pageTemp", new PageTempManager(sortInfoManager, metadataManager));
        temps.put("actionTemp", new ActionTempManager(sortInfoManager, metadataManager));
        temps.put("configTemp", new ConfigTempManager(sortInfoManager, metadataManager));
        temps.put("otherTemp", new OtherTempManager(sortInfoManager, metadataManager));
        temps.put("entityTemp", new EntityTempManager(sortInfoManager, metadataManager));
    }

    public static BuildProjectFactory instance() {
        if (null == instance) {
            instance = new BuildProjectFactory();
        }
        return instance;
    }

    @SuppressWarnings("rawtypes")
    public boolean ctreateProject(Map<String, String> param) {
        String createTemplate = param.get("createTemplate");
        boolean mark = false;
        Map<String, Object> map = new HashMap<String, Object>();
        map.putAll(param);
        ParamsVo vo = new ParamsVo();
        vo.setParams("sortIds", param.get("ids"));
        List list = (List) sortInfoManager.getList(vo);
        if (ToolsUtil.isEmpty(list)) {
            log.info("没有获取到数据表信息");
            return mark;
        }
        map.put("sortList", list);
        if ("0".equals(createTemplate)) {//全部
            mark = temps.get("pageTemp").createPage(map);
            //mark = temps.get("actionTemp").createAction(map);
            mark = temps.get("entityTemp").createEntity(map);
            mark = temps.get("otherTemp").createOther(map);
            mark = temps.get("configTemp").createConfig(map);
            otherInfo(map);
        } else if ("1".equals(createTemplate)) {//页面
            mark = temps.get("pageTemp").createPage(map);
        } else if ("2".equals(createTemplate)) {//配置文件
            mark = temps.get("configTemp").createConfig(map);
        } else if ("3".equals(createTemplate)) {//action
            //mark = temps.get("actionTemp").createAction(map);
        } else if ("4".equals(createTemplate)) {//entity
            mark = temps.get("entityTemp").createEntity(map);
        } else if ("5".equals(createTemplate)) {//其他
            mark = temps.get("otherTemp").createOther(map);
        }
        return mark;
    }

    /**
     * description:  生成工程配置文件
     */
    private void otherInfo(Map param) {
        String jarPath = String.valueOf(param.get("packPath"));
        String prefix = String.valueOf(param.get("projectName")) + "/";
        String xmlPath = String.valueOf(param.get("filePath")) + "/" + prefix;
        FileOperate.getInstance().newCreateFolder(xmlPath + "/");
        FileOperate.getInstance().newCreateFolder(xmlPath + "/src/site/");
        FileOperate.getInstance().newCreateFolder(xmlPath + "/src/test/resources/");
        FileOperate.getInstance().newCreateFolder(xmlPath + "/src/test/java/");
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("packPath", jarPath);
        data.put("projectName", String.valueOf(param.get("projectName")));
        try {
            String result = FreemarkerHelper.instance().getTemplateStr("xml/pom.inc", data);
            FileOperate.getInstance().newCreateFile(xmlPath + "pom.xml", result);
            data.put("bodyReports", "${reports}");
            result = FreemarkerHelper.instance().getTemplateStr("xml/site.inc", data);
            FileOperate.getInstance().newCreateFile(xmlPath + "src/site/site.xml", result);
        } catch (Exception e) {
            log.error("生成xml文件失败", e);
        }
    }
}
